<?php

// +----------------------------------------------------------------------
// |快递服务层
// +----------------------------------------------------------------------

namespace app\Api\system;

use think\facade\Validate;
use think\facade\Db;
use think\facade\Cache;
use app\common\Model\ConfigModel;

class GetShippingService
{  

    //dh.shipping.templates.get$1.0 (卖家获取运费模版列表接口)
    public static function getTemplates($accessToken,$shopid){   
    
        $method='dh.shipping.templates.get';
        $sysParams = [
            'method' => $method,
            'v' => '1.0',
            'access_token' => $accessToken,
            'timestamp' => round(microtime(true) * 1000),    
        ];
        $res=crul($sysParams);
        $data=[];
        if($res['shippingModelList']){            
            foreach ($res['shippingModelList'] as $v) {
                $data[]=[
                    'shippingmodelid'=>$v['shippingmodelid'],//敦煌运费模板id
                    'modelname'=>$v['modelname'],// 模板名称
                    'supplierid'=>$v['supplierid'],//卖家id
                    'isdefault'=>$v['isdefault'],//是否默认模版 1是,0不是
                    'shop_id'=>$shopid,//店铺id
                    'isDHPort'=>$v['isDHPort'],//是否是海外直发模版 1是  0不是
                    'modeltype'=>$v['modeltype'],//模板类型 00新手运费模板 01用户模板
                    'modelitems'=>$v['modelitems'],//模板描述
                    'createymd'=> date('Ymd'),//添加时间
                    'createtime'=>date('y-m-d H:i:s')//添加时间  
                ];
            }
        }
        Db::startTrans();
        try{
            if($data){
                Db::name('shops')->where('id',$shopid)->update(['shippingmodelid'=>$res['shippingModelList'][0]['shippingmodelid']]);
                Db::name('logistice_template')->insertAll($data);
            
            }
            Db::commit();
                echo '成功添加数据';die;
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            // 记录错误或返回错误信息
            print_r('处理失败'.$e->getMessage());die;
        }             
    }
    
}